
package generadorDescriptores.forms.creation;

import generadorDescriptores.Fachada;
import generadorDescriptores.forms.edition.FormEditarJoins;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JDialog;
import javax.swing.JFrame;

public class FormNewMetricFunction2 extends javax.swing.JDialog {

	//Form3Metric form3metric;
	String newFunction = "";
	ArrayList<String> fields;
	
	/** Creates new form FormNewMetricFunction 
	 * @param fields */
	public FormNewMetricFunction2(JFrame frame, boolean modal, ArrayList<String>fields) {
		super(frame, modal);
		//this.form3metric = form;
		this.fields = fields;
		initComponents();
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.listFields.setMultipleMode(false);
		this.fillFieldList();
		this.mouseListener();
		//this.form3metric.setEnabled(false);
	}

	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jSeparator1 = new javax.swing.JSeparator();
		listFields = new java.awt.List();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		TextFieldFunction = new javax.swing.JTextField();
		ButtonAceptar = new javax.swing.JButton();
		ButtonDescartar = new javax.swing.JButton();
		jLabel4 = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
		jLabel1.setText("Ingrese la nueva funci\u00f3n:");

		jLabel2.setText("Campos:");

		jLabel3.setText("Escriba la nueva funci\u00f3n:");

		ButtonAceptar.setText("Aceptar");
		ButtonAceptar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				ButtonAceptarActionPerformed(evt);
			}
		});

		ButtonDescartar.setText("Descartar");
		ButtonDescartar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				ButtonDescartarActionPerformed(evt);
			}
		});

		jLabel4
				.setText("  (Si hace doble click sobre un campo se agregar\u00e1 a la funci\u00f3n).");

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.TRAILING)
														.add(
																org.jdesktop.layout.GroupLayout.LEADING,
																jLabel4,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																304,
																Short.MAX_VALUE)
														.add(
																org.jdesktop.layout.GroupLayout.LEADING,
																layout
																		.createParallelGroup(
																				org.jdesktop.layout.GroupLayout.TRAILING,
																				false)
																		.add(
																				org.jdesktop.layout.GroupLayout.LEADING,
																				layout
																						.createSequentialGroup()
																						.add(
																								ButtonAceptar,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																								144,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
																						.add(
																								18,
																								18,
																								18)
																						.add(
																								ButtonDescartar,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE))
																		.add(
																				org.jdesktop.layout.GroupLayout.LEADING,
																				listFields,
																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.add(
																				org.jdesktop.layout.GroupLayout.LEADING,
																				jSeparator1)
																		.add(
																				org.jdesktop.layout.GroupLayout.LEADING,
																				jLabel1,
																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.add(
																				org.jdesktop.layout.GroupLayout.LEADING,
																				jLabel2)
																		.add(
																				org.jdesktop.layout.GroupLayout.LEADING,
																				jLabel3)
																		.add(
																				org.jdesktop.layout.GroupLayout.LEADING,
																				TextFieldFunction)))
										.addContainerGap(38, Short.MAX_VALUE)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.add(jLabel1)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												jSeparator1,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												10,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(jLabel2)
										.add(8, 8, 8)
										.add(
												listFields,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(5, 5, 5)
										.add(jLabel4)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.UNRELATED)
										.add(jLabel3)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.UNRELATED)
										.add(
												TextFieldFunction,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED,
												45, Short.MAX_VALUE)
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(ButtonAceptar)
														.add(ButtonDescartar))
										.addContainerGap()));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

		
	private void ButtonDescartarActionPerformed(java.awt.event.ActionEvent evt) {
		this.setVisible(false);
	}

	private void ButtonAceptarActionPerformed(java.awt.event.ActionEvent evt) {
		Fachada.getInstance().setNewFunctionMetric(TextFieldFunction.getText());
		this.dispose();		
	}

	private void mouseListener() {
		this.listFields.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {

				if (e.getClickCount() == 2) {
					addField();
				}
			}
		});
	}

	private void addField() {
		String field = this.listFields.getSelectedItem().toString();
		String aux = this.TextFieldFunction.getText();
		this.TextFieldFunction.setText(aux + " " + field);
	}
	

	private void fillFieldList() {
		//ArrayList<String> fields = this.form3metric.getFields();
		for (int i = 0; i < fields.size(); i++) {
			listFields.addItem(fields.get(i));
		}
	}



	/**
	 * @param form3Metric 
	 * @param form3Metric the command line arguments
	 */
	public static void main(final ArrayList<String> fields, final Form3Metric form3Metric) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				FormNewMetricFunction2 dialog = new FormNewMetricFunction2(
						form3Metric, true, fields);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					public void windowClosing(java.awt.event.WindowEvent e) {

					}
				});
				dialog.setVisible(true);	
			
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton ButtonAceptar;
	private javax.swing.JButton ButtonDescartar;
	private javax.swing.JTextField TextFieldFunction;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JSeparator jSeparator1;
	private java.awt.List listFields;
	// End of variables declaration//GEN-END:variables

}